package com.wzb.mybatisplus.bitaction.formal;

import com.wzb.mybatisplus.bitaction.ArchiveThreadPool;
import com.wzb.mybatisplus.bitaction.BitcoinPrivateKeyGenerator1;
import com.wzb.mybatisplus.bitaction.electrumaction.one.BitcoinKeyConverter;
import com.wzb.mybatisplus.bitaction.electrumaction.one.BlockchainBalanceChecker;
import com.wzb.mybatisplus.bitaction.electrumaction.one.KeyPair;
import com.wzb.mybatisplus.bitaction.electrumaction.three.SendEmailToQQ;
import com.wzb.mybatisplus.entity.Bituse;
import org.bitcoinj.core.*;
import org.bitcoinj.params.MainNetParams;
import org.bitcoinj.script.Script;
import org.bitcoinj.script.ScriptBuilder;
import org.json.JSONObject;

import java.math.BigInteger;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.security.SecureRandom;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.locks.LockSupport;
 // 取模115792089237316195423570985008687907853269984665640564039457584007908834671663
//   0-115792089237316195423570985008687907852837564279074904382605163141518161494337
public class test {
     /**
      * 将最高位设置为0（清除最高位）
      * @param number 原始数字
      * @return 清除最高位后的结果
      */
     public static BigInteger clearHighestBit(BigInteger number) {
         if (number.equals(BigInteger.ZERO)) {
             return BigInteger.ZERO;
         }

         int bitLength = number.bitLength();
         if (bitLength == 0) {
             return BigInteger.ZERO;
         }

         // 创建掩码：除了最高位为0，其他位为1
         // 例如：对于 101010 (42)，bitLength=6，掩码为 011111 (31)
         BigInteger mask = BigInteger.ONE.shiftLeft(bitLength - 1).subtract(BigInteger.ONE);
         return number.and(mask);
     }
    private static final NetworkParameters MAINNET = MainNetParams.get();
    public static void main(String[] args) {

        System.out.println(new BigInteger("115792089237316195423570985008687907853269984665640564039457584007908834671663").divide(BigInteger.valueOf(1000000)));
//        System.out.println(new BigInteger("C28DCAF45F3A6FCCF7F70BE6A75F5F6F6A75F5F6F6A75F5F6F6A75F5F6F6A75F",16));
//                         System.out.println("87999218802305177194863145464757464116623021623585035533703579771544440055647".length());
////        System.out.println(new BigInteger("77194726158210796949047323339125271901891709519383269588403442094345440").divide(new BigInteger("2")));
////        System.out.println(new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141",16).divide(new BigInteger("1500000")));
////        System.out.println((new BigInteger("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141",16)+""));
////        System.out.println("115792089237316195423570985008687907852837564279074904382605163141518".length());
//        test test1 = new test();
//        sy.clear();
//        int randomInt = (int)(Math.random() * 1000000000);
//        BigInteger c = new BigInteger("0");
//        BitcoinPrivateKeyGenerator1.BitcoinPrivateKey privateKey = BitcoinPrivateKeyGenerator1.generatePrivateKey();
//        BigInteger bigInteger = privateKey.getPrivateKey();
//        sy.add(privateKey.getWIFUncompressed());
//        sy.add(privateKey.getWIFCompressed());
//        for(int i =0;i<10;i++) {
//
////            randomInt = (int)(Math.random() * 1000000000);
////            BigInteger d = BigInteger.valueOf(randomInt).multiply(BigInteger.valueOf(randomInt));
////            BigInteger num = new BigInteger("35792089237316195423570985078687907852837564279074904382605163141518161494338").subtract(BigInteger.valueOf(i));
////            test1.ac(bigInteger.subtract(d));
//            System.out.println("i：");
//            test1.ac(BigInteger.valueOf(i+1));
//        }//                              115792089237316195423570985008687907852837564279074904382605163141518161494336
//        BigInteger a = new BigInteger("115792089237316195423570985008687907852837564279074904382605163141518161494337");
//        BigInteger b = new BigInteger("57896044618658097711785492504343953926202571946254622362876371137561596674370");
//        BigInteger c = new BigInteger("86844066927987146567678238756515930889520068112664763372740767139539879084353");
//        System.out.println("101318078082651670995624611882601919371178816195869833877672965140529020289345");
//
//        System.out.println(clearHighestBit(a));
//        System.out.println(b.divide(BigInteger.TEN));
//        System.out.println((a.add(c)).divide(BigInteger.TWO));
//
//        try {
//            // 创建HTTP客户端（会自动使用系统VPN设置）
//            HttpClient client = HttpClient.newBuilder()
//                    .connectTimeout(Duration.ofSeconds(10))
//                    .build();
//
//            // 构建请求 - 获取比特币区块数据
//            String apiUrl = "https://blockchain.info/balance?active=1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa";
//            HttpRequest request = HttpRequest.newBuilder()
//                    .uri(URI.create(apiUrl))
//                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
//                    .timeout(Duration.ofSeconds(15))
//                    .GET()
//                    .build();
//
//            // 发送请求并获取响应
//            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
//
//            // 输出结果
//            System.out.println("HTTP Status: " + response.statusCode());
//            System.out.println("Response Body: " + response.body());
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
    public static Set<String> sy = new HashSet<>();
    public void ac(BigInteger num){
        int j=0;
//        System.out.println(new BigInteger("115792089237316195423570985008687907852837564279074904382605163141518161494336"));

        //随机10个亿  1000000000
//        int randomInt = (int)(Math.random() * 1000000000);
//        BigInteger pub=new BigInteger("631404");
        BigInteger pub=num;
//        pub = BitcoinShorSimulation.Gy;
//        System.out.println((pub+"").length());
        for(int i=1;i<2;i++,j++) {
//            for(int i=1;i<5000;i++,j++) {
            if(j>=5000){break;}
            //随机10个亿  1000000000
            int randomInt = (int)(Math.random() * 1000000000);
//            BitcoinPrivateKeyGenerator1.BitcoinPrivateKey privateKey = BitcoinPrivateKeyGenerator1.generatePrivateKey();
            BitcoinPrivateKeyGenerator1.BitcoinPrivateKey privateKey = BitcoinPrivateKeyGenerator1.generatePrivateKey(pub, BigInteger.valueOf(1));
            if(privateKey==null){ pub = clearHighestBit(pub);continue;}
            // 私钥转公钥 k
            String wifPrivateKey = privateKey.getWIFCompressed();

            KeyPair wifKeyPair = null;
            try {
                wifKeyPair = BitcoinKeyConverter.generateFromWIF(wifPrivateKey);
            } catch (Exception e) {

            }
            if(wifKeyPair==null){
//               sy = new BigInteger(privateKey.getHex(),16).add( BigInteger.valueOf(1));
                continue;
            }

            String address = wifKeyPair.getLegacyAddress(); // 比特币创始区块地址公钥地址
            String publicKey = wifKeyPair.getPublicKeyCompressed();
            publicKey = publicKey.substring(2);
            System.out.println("公钥:"+new BigInteger(publicKey,16));
            System.out.println("私钥:"+privateKey.getPrivateKey());
//            pub = clearHighestBit(pub);
            if(("L5oLkpV3aqBjhki6LmvChTCV6odsp4SXM6FfU2Gppt5kFLaHLuZ9").equals(wifPrivateKey)){
                System.out.println("=======");
            }
//            BitcoinPrivateKeyGenerator1.BitcoinPrivateKey privateKey1 = BitcoinPrivateKeyGenerator1.generatePrivateKey(pub, BigInteger.valueOf(1));
                sy.add(privateKey.getWIFUncompressed());
            sy.add(privateKey.getWIFCompressed());
//           System.out.println(i+"地址："+address +"\n16进制："+publicKey+"\n10进制"+pub);


//        System.out.println("地址: " + key.toAddress(org.bitcoinj.params.MainNetParams.get()));
        }

        for (String s : sy){
            System.out.println(s);
        }

    }

    private void aa(){
        System.out.println();
        Set<String> sy = new HashSet<>();
        Set<String> num = new HashSet<>();
        for(int i=0;sy.size()<3000;i++) {
            BigInteger pub = new BigInteger(ab(255),2);
//            System.out.println((pub+"").length());

            BitcoinPrivateKeyGenerator1.BitcoinPrivateKey privateKey = BitcoinPrivateKeyGenerator1.generatePrivateKey(pub, BigInteger.valueOf(1));
            if(privateKey==null){i--;continue;}
            String wifPrivateKey = privateKey.getWIFCompressed();

            KeyPair wifKeyPair = null;
            try {
                wifKeyPair = BitcoinKeyConverter.generateFromWIF(wifPrivateKey);
            } catch (Exception e) {

            }
            if(wifKeyPair==null){
//               sy = new BigInteger(privateKey.getHex(),16).add( BigInteger.valueOf(1));
                continue;
            }
            num.add(pub+"");
            sy.add(privateKey.getWIFCompressed());
        }
//        num.stream().forEach(System.out::println);
//        System.out.println("==================");
        sy.stream().forEach(System.out::println);
    }
    // 随机256个
    private String ab(int max){
        String s = "";
        for(int i=0;i<=max;i++){
            int j  = new Random().nextInt(100000000);

            s+=(j%2);
        }
        return s;
    }
}
